package com.souche.employment.controller;

import com.souche.employment.cache.RedisService;
import com.souche.employment.domain.UserDO;
import com.souche.employment.dto.LoginDto;
import com.souche.employment.interceptor.AuthHolder;
import com.souche.employment.mapper.UserMapper;
import com.souche.employment.response.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.UUID;

/**
 * BaseController.java created at 2017年8月8日 下午1:11:20
 *
 * @author Ray [email:rayinhangzhou@hotmail.com]
 * Revision History
 * Date            Programmer                   Notes
 * ----------------------------------------------------------
 **/

@RestController
@Api(value = "/pub", description = "公用接口")
@RequestMapping(value = "/pub")
public class PubController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisService redisService;


    @RequestMapping(value = "/getPwd", method = RequestMethod.GET)
    @ApiOperation(value = "获取验证码")
    public DataResult getPwd(@RequestParam String mail) {
        String code = "1234";
        //TODO 发邮件
        redisService.set("loginCode" + mail, code, 30 * 60);
        return DataResult.success(code);
    }

    @RequestMapping(value = "/getLogin", method = RequestMethod.POST)
    @ApiOperation(value = "获取用户")
    public DataResult getLogin(@RequestParam String mail, @RequestParam String password, HttpServletResponse response) {
        if (mail.contains("souche\\.com")) {
            return DataResult.fail("请使用大搜车完整邮箱作为账号", null);
        }
        UserDO userDO = userMapper.getUserByLoginMail(mail);
        if (userDO == null) {
            return DataResult.fail("账号不存在，请带上饮料联系管理员", null);
        }
        String token = UUID.randomUUID().toString();
        LoginDto loginDto = new LoginDto();
        String tip = (String) redisService.hget(RedisService.KEY_LAST_LOGIN, mail);
        if (StringUtils.isNotBlank(tip)) {
            loginDto.setTip("上一次登录时间："+tip);
        }
        loginDto.setMail(mail);
        loginDto.setName(userDO.getName());
        loginDto.setType(userDO.getType());
        loginDto.setToken(token);
        //TODO 处理redis
        redisService.hset(RedisService.KEY_LAST_LOGIN, mail, DateFormatUtils.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss"));
        redisService.set(token, loginDto, 12 * 60 * 60);
        // 处理 cookies
        Cookie cookie = new Cookie("TT", token);
        cookie.setPath("/");
        response.addCookie(cookie);
        return DataResult.success(loginDto);
    }

    @RequestMapping(value = "/getLogout", method = RequestMethod.POST)
    @ApiOperation(value = "登出")
    public DataResult getLogout() {
        redisService.delete(AuthHolder.mail());
        redisService.delete(AuthHolder.token());
        return DataResult.success(null);
    }


}
